package com.java.auth.controller;

import com.java.auth.service.SysMenuService;
import com.java.common.utils.R;
import com.java.model.system.SysMenu;
import com.java.vo.system.AssginMenuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/admin/system/sysMenu")
@Api(tags = "菜单管理")
public class SysMenuController {
    @Autowired
    private SysMenuService sysMenuService;

    @ApiOperation(value = "获取菜单")
    @GetMapping("findNodes")
    public R findNodes() {
        return sysMenuService.findNodes();
    }
    @PreAuthorize("hasAuthority('bnt.sysMenu.add')")
    @ApiOperation(value = "新增菜单")
    @PostMapping("save")
    public R save(@RequestBody SysMenu permission) {
        sysMenuService.save(permission);
        return R.ok();
    }

    @PreAuthorize("hasAuthority('bnt.sysMenu.update')")
    @ApiOperation(value = "修改菜单")
    @PutMapping("update")
    public R updateById(@RequestBody SysMenu permission) {
        sysMenuService.updateById(permission);
        return R.ok();
    }

    @PreAuthorize("hasAuthority('bnt.sysMenu.remove')")
    @ApiOperation(value = "删除菜单")
    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable Long id) {
        return sysMenuService.removeById(id);
    }

    @PreAuthorize("hasAuthority('bnt.sysMenu.list')")
    @ApiOperation(value = "根据角色获取菜单")
    @GetMapping("toAssign/{roleId}")
    public R findMenuByRole(@PathVariable Long roleId){
        return sysMenuService.findMenuByRole(roleId);
    }
    @PreAuthorize("hasAuthority('bnt.sysRole.assignAuth')")
    @ApiOperation(value = "给角色分配权限")
    @PostMapping("/doAssign")
    public R assignPermToRole(@RequestBody AssginMenuVo assignMenuVo){
        return sysMenuService.assignPermToRole(assignMenuVo);
    }
}
